package android.app.betmein.view;

import items.BetItem;
import items.BetSideItem;
import items.EntityItem;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;

import model.BetSide;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.betmein.R;
import android.content.DialogInterface;
import android.framework.binding.BindableValue;
import android.framework.binding.Binding;
import android.framework.binding.BindingTextView;
import android.framework.binding.IViewCreator;
import android.framework.binding.MethodRepresent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;

public class ViewCreatorHelper {
	public static IViewCreator<EntityItem> entityListViewCreator() {
		return new IViewCreator<EntityItem>() {

			public void createView(int position, View convertView,
					ViewGroup parent, EntityItem item,  HashMap<Integer, MethodRepresent> methodById) {
				BindingTextView entityName = (BindingTextView) convertView
						.findViewById(R.id.entityName);
				BindingTextView entityDetails = (BindingTextView) convertView
						.findViewById(R.id.entityDetails);
				final ImageButton rightImageButton = (ImageButton) convertView
						.findViewById(R.id.rightimageButton);
				rightImageButton.setFocusable(false);
				rightImageButton.setFocusableInTouchMode(false);
				rightImageButton.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {

						rightImageButton
								.setBackgroundResource(R.drawable.ic_action_search);
					}
				});

				Binding.attached(item.getEntityName(), entityName);
				Binding.attached(item.getCenteralTextView(), entityDetails);
				rightImageButton.setBackgroundResource(item
						.getRightImageButton());
				rightImageButton.setClickable(true);

			}
		};
	}
	
	public static IViewCreator<BetSideItem> betSideListViewCreator() {
		return new IViewCreator<BetSideItem>() {

			public void createView(int position, View convertView,
					ViewGroup parent, BetSideItem item,  HashMap<Integer, MethodRepresent> methodById) {
				BindingTextView betDescription = (BindingTextView) convertView
						.findViewById(R.id.cb_addSideDescription);
				betDescription.setText(item.getSideDescription().getText());
			}
		};
	}
	
	public static IViewCreator<BetItem> betListViewCreator() {
		return new IViewCreator<BetItem>() {

			public void createView(int position, final View convertView,
					ViewGroup parent, final BetItem item, final HashMap<Integer, MethodRepresent> methodById) {
				BindingTextView title = (BindingTextView)convertView.findViewById(R.id.title);
				BindingTextView entityName = (BindingTextView)convertView.findViewById(R.id.entityName);
				Button takeSideButton = (Button)convertView.findViewById(R.id.takeSideButton);
				Button beAJudgeButton = (Button)convertView.findViewById(R.id.beAJudge);
				Button rejectButton = (Button)convertView.findViewById(R.id.reject);
				
				
				List<BetSideItem> betSides = item.getBetSides();
				final ArrayAdapter<BetSideItem> betSideAdapter = new ArrayAdapter<BetSideItem>(parent.getContext(), R.layout.single_bet_side, R.id.betSideTitle, betSides);
				
				Binding.attached(item.getTitle(), title);
				Binding.attached(item.getEntityName(), entityName);
				
				rejectButton.setFocusable(false);
				rejectButton.setFocusableInTouchMode(false);
				rejectButton.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						if(methodById != null && methodById.containsKey(R.id.reject)){
							MethodRepresent methodRepresent = methodById.get(R.id.reject);
							invokeMethod(methodRepresent.method, methodRepresent.owner, item);
						}
					}
				});
				
				beAJudgeButton.setFocusable(false);
				beAJudgeButton.setFocusableInTouchMode(false);
				beAJudgeButton.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						if(methodById != null && methodById.containsKey(R.id.beAJudge)){
							MethodRepresent methodRepresent = methodById.get(R.id.beAJudge);
							invokeMethod(methodRepresent.method, methodRepresent.owner, item);
						}
					}
				});
				
				takeSideButton.setFocusable(false);
				takeSideButton.setFocusableInTouchMode(false);
				takeSideButton.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						AlertDialog.Builder builder = new Builder(convertView.getContext());
						builder.setTitle(R.string.takeSide);
						builder.setSingleChoiceItems(betSideAdapter, -1, new DialogInterface.OnClickListener() {
							
							public void onClick(final DialogInterface dialog, final int which) {
								AlertDialog areUSure = new Builder(convertView.getContext()).setMessage(R.string.are_u_sure).setCancelable(true)
										.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
								               public void onClick(final DialogInterface innerDialog, final int id) {
								            	   dialog.dismiss();
													if(methodById != null && methodById.containsKey(R.id.takeSideButton)){
														MethodRepresent methodRepresent = methodById.get(R.id.takeSideButton);
														invokeMethod(methodRepresent.method, methodRepresent.owner, item, betSideAdapter.getItem(which));
													}
								               }
								           })
								           .setNegativeButton("No", new DialogInterface.OnClickListener() {
								               public void onClick(final DialogInterface innerDialog, final int id) {
								            	   innerDialog.cancel();
								               }
								           }).create();
								
								areUSure.show();
								
								
							}
						});
						builder.create().show();
					}
				});
				
				
			}
		};
	}
	
	public static IViewCreator<BetItem> judgmentBetListViewCreator() {
		return new IViewCreator<BetItem>() {

			public void createView(int position, final View convertView,
					ViewGroup parent, final BetItem item, final HashMap<Integer, MethodRepresent> methodById) {
				BindingTextView title = (BindingTextView)convertView.findViewById(R.id.title);
				BindingTextView entityName = (BindingTextView)convertView.findViewById(R.id.entityName);
				
				Binding.attached(item.getTitle(), title);
				Binding.attached(item.getEntityName(), entityName);
			}
		};
	}
	
	public static IViewCreator<BetItem> participateBetListViewCreator() {
		return new IViewCreator<BetItem>() {

			public void createView(int position, final View convertView,
					ViewGroup parent, final BetItem item, final HashMap<Integer, MethodRepresent> methodById) {
				BindingTextView title = (BindingTextView)convertView.findViewById(R.id.title);
				BindingTextView entityName = (BindingTextView)convertView.findViewById(R.id.entityName);
				final Button betSideButton = (Button)convertView.findViewById(R.id.chooseSideButton);
				
				Binding.attached(item.getTitle(), title);
				Binding.attached(item.getEntityName(), entityName);
				Binding.attached(item.getChoosenSide().getValue().getBetSide(), new BindableValue<BetSide>(null){
					public void notifyChanges(BetSide value) {
						super.notifyChanges(value);
						if(value != null){
							betSideButton.setText(value.getSideDescription().getText());
						}
					}
				});
				betSideButton.setText(item.getChoosenSide().getValue().getSideDescription().getText());
				betSideButton.setFocusable(false);
				betSideButton.setFocusableInTouchMode(false);
				
				List<BetSideItem> betSides = item.getBetSides();
				final ArrayAdapter<BetSideItem> betSideAdapter = new ArrayAdapter<BetSideItem>(parent.getContext(), R.layout.single_bet_side, R.id.betSideTitle, betSides);
				
				betSideButton.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						AlertDialog.Builder builder = new Builder(convertView.getContext());
						builder.setTitle(R.string.takeSide);
						builder.setSingleChoiceItems(betSideAdapter, -1, new DialogInterface.OnClickListener() {
							public void onClick(final DialogInterface dialog, final int which) 
							{
								dialog.dismiss();
								if(methodById != null && methodById.containsKey(R.id.chooseSideButton)){
									MethodRepresent methodRepresent = methodById.get(R.id.chooseSideButton);
									invokeMethod(methodRepresent.method, methodRepresent.owner, item, betSideAdapter.getItem(which));
								}
							}
						});
						
						builder.create().show();
					}
				});
			}
		};
	}
	
	
	
	private static void invokeMethod(Method method, Object owner, Object ... args){
		try {
			method.setAccessible(true);
			method.invoke(owner, args);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
